home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Utilities / Programming / C Reference Card 2.5 / C Reference Card / C Reference Card.rsrc / TEXT_412_12.txt < prev    next >
Encoding:
Text File  |  1996-06-24  |  5.0 KB  |  154 lines

  1. PREPROCESSING : commands, file inclusion, macros, conditional ...
  2. _________________________________________________________________________
  3.  
  4.  
  5. PREPROCESSOR COMMANDS
  6.  
  7. #define      Define a preprocessor macro.
  8.  
  9.              Usage: #define MAX 256
  10.  
  11. #undef       Remove a preprocessor macro definition.
  12.  
  13.              Usage: #undef MAX
  14.  
  15. #include     Insert text from another source file.
  16.  
  17.              Usage: #include <file>  ->searches 'standard' locations
  18.                     #include "file"  ->searches 'local' locations
  19.  
  20. #if          Conditionally include some text, based on the value of a
  21.              constant expression.
  22.  
  23.              Usage: #if MAX > 256
  24.                       #include "altSize.h"
  25.                     #endif
  26.  
  27. #ifdef       Conditionally include some text, based on whether a macro
  28.              name is defined.
  29.  
  30. #ifndef      Conditionally include some text, based on whether a macro
  31.              name is not defined.
  32.  
  33.              Usage: #ifndef MAX
  34.                       #define MAX 256
  35.                     #endif
  36.  
  37. #else        Alternatively include some text, if the previous #if, 
  38.              #ifdef, #ifndef, or #elif test failed.
  39.  
  40. #endif       Terminate conditional text.
  41.  
  42. #line        Supply a line number for compiler messages.
  43.  
  44. #elif        Alternatively include some text based on the value of
  45.              another constant expression, if the previous #if, #ifdef,
  46.              #ifndef, or #elif test failed.
  47.  
  48. defined      Preprocessor function that yields 1 if a name is defined
  49.              as a preprocessor macro and 0 otherwise; used in #if and 
  50.              #elif statements.  
  51.  
  52.              Usage: #if defined TOKEN -or- #if defined(TOKEN)
  53.  
  54. unary #      Operator to replace macro parameter with a string constant
  55.              containing the parameter's value.
  56.  
  57.              Usage: #define PRINT(a) printf( "value = " #a "\n")
  58.  
  59.              'PRINT(5);' becomes 'printf("value = 5\n");'
  60.  
  61. binary ##    Operator to create a single token out of two adjacent
  62.              tokens.
  63.  
  64.              Usage: #define INPUT(i) input ## i
  65.  
  66.              'INPUT(1) = INPUT(2);' becomes 'temp1 = temp2;'
  67.  
  68. #pragma      Specify implementation-dependent information to the
  69.              compiler.  Although it's best not to have implementation-
  70.              dependent pre-processing statements, it's a very 
  71.              pragmatic inclusion to the list of preprocessing commands
  72.              (hence it's name).
  73.  
  74. #error       Produce a compile-time error with a designated message.
  75.  
  76.              Usage: #error "Opps! MAX not Defined."
  77.  
  78.  
  79.  
  80. FILE INCLUSION
  81.  
  82. You use the '#include' preprocessing directive to include header files.  The entire contents of the specified file is inserted in place of the file inclusion statement.  The following are some examples:
  83.  
  84.   #include <iostream.h>
  85.   #include <stdlib.h>
  86.   #include "myHeader.h"
  87.  
  88. The '<>' brackets identify standard libraries which are part of the compiler and follow implementation-defined rules to find the file.  The double-quote brackets define local files (usually within the directory or sub-directories that your project file is located in).
  89.  
  90.  
  91.  
  92. MACROS
  93.  
  94. Macros use the '#define' preprocessing directive to substitute any occurrence of the macro 'name' with the identified text in the macro definition.  Examples include:
  95.  
  96.   #define MAX_CHARS 255
  97.   #define ERROR_STR "\pError, try again."
  98.   #define MAX(A,B)  ( (A)>(B) ? (A) : (B) )
  99.  
  100.  
  101.  
  102. CONDITIONAL INCLUSION
  103.  
  104. You can use conditional inclusion preprocessing directives to define or include items which are compiler or operating system specific.
  105.  
  106.   #ifndef OS
  107.   #define OS MAC
  108.   #endif
  109.  
  110.   #if OS == MAC
  111.     #define SYS_HEADER "MacOS.h"
  112.   #elif OS == WIN
  113.     #define SYS_HEADER "Win.h"
  114.   #elif OS == WIN95
  115.     #define SYS_HEADER "Win95.h"
  116.   #elif OS == UNIX
  117.     #define SYS_HEADER "Unix.h"
  118.   #endif
  119.   #include SYS_HEADER
  120.  
  121.  
  122.  
  123. PRAGMA DIRECTIVES
  124.  
  125. Pragma directives are compiler-specific and therefore, tend to be less portable.  The format for THINK C pragma directives is:
  126.  
  127.   #pragma [SC] pragma_directive [pragma_args]
  128.  
  129. If you specify SC, the directive must be recognized by Symantec C/C++ compilers.  Some examples of THINK C pragma directives include:
  130.  
  131.   #pragma [SC] align [1/2/4]         // sets byte alignments within
  132.                                      // structures.
  133.  
  134.   #pragma [SC] message "text"        // prints text while compiling.
  135.  
  136.   #pragma [SC] once                  // when included in a header file,
  137.                                      // file is included only once even
  138.                                      // if #include directives include
  139.                                      // it multiple times.
  140.  
  141.   #pragma [SC] options align=power   // equivalent to align 4 directive.
  142.   #pragma [SC] options align=native
  143.  
  144.   #pragma [SC] options align=mac68k  // equivalent to align 2 directive.
  145.  
  146.   #pragma [SC] options align=reset   // default alignment.
  147.  
  148.   #pragma [SC] template class<args>  // produces instantiations of a
  149.                                      // template.
  150.  
  151.   #pragma [SC] template_access code  // code type can be public, extern,
  152.                                      // or static.
  153.  
  154.